package minha.lista;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String>
{
  private final Context context;
  private final List<String> values;

  public MySimpleArrayAdapter(Context context, List<String> values)
  {
    super(context, R.layout.file_layout, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    View v = convertView;
    LayoutInflater inflater = LayoutInflater.from(context);

    v = inflater.inflate(R.layout.file_layout, null);


    TextView title = (TextView) v.findViewById(R.id.file_name);

    File f = new File(values.get(position));
    title.setText(f.getName());
    // Change the icon
    if (f.isDirectory())
    {
      v = inflater.inflate(R.layout.test_layout, null);

      ImageView icon = (ImageView) v.findViewById(R.id.folder_art);
      TextView subtitle = (TextView) v.findViewById(R.id.folder_subtitle);


      title = (TextView) v.findViewById(R.id.folder_name);
      title.setText(f.getName());
      subtitle.setText("xxx");

      File[] subfiles = f.listFiles();

      for (File sub : subfiles)
      {
        if (sub.getName().toLowerCase().endsWith(".jpg"))
        {
          icon.setImageBitmap(BitmapFactory.decodeFile(sub.getAbsolutePath()));
          break;
        }

      }
    }

    /*
     * else imageView.setImageResource(R.drawable.music_ico2);
     */

    return v;
  }

  static class ViewHolder
  {
    TextView album_size;
  }
}
